<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>

    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="submit">确定</el-button>
      <el-button @click="$emit('success')">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/settings'
import { getUserInfo } from '@/api/user'
import { assignRole } from '@/api/employess'
export default {
  props: {
    culId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.loadAssignRole()
  },
  methods: {
    // 点击确定按钮分配权限
    async submit() {
      try {
        await assignRole({ id: this.culId, roleIds: this.roleIds })
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    // 获取所有的角色权限列表
    async loadAssignRole() {
      const res = await getRoles({ page: 1, pagesize: 100 })
      // console.log(res)
      const detail = await getUserInfo(this.culId)
      this.roleIds = detail.data.roleIds
      console.log(detail)
      this.list = res.data.rows
    }
  }
}
</script>
